package com.qxglxt.qxglxt.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.qxglxt.qxglxt.dao.IUsersDao;
import com.qxglxt.qxglxt.dto.UsersDTO;
import com.qxglxt.qxglxt.model.Roles;
import com.qxglxt.qxglxt.model.Users;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements IUsersService {

    @Resource
    private IUsersDao usersDao;

    public Page<Users> findAllPage(UsersDTO dto) {
        Sort sort = Sort.by(Sort.Order.asc("id"));//根据ID排序
        Pageable page = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), sort);
        return usersDao.findAll(getWhereClause(dto), page);
    }

    PRIVATE Specification<Users> getWhereClause(final UsersDTO dto) {
        return new Specification<Users>(){
            /**
			 *
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate predicate=criteriaBuilder.conjunction();//动态sql查询
                List<Expression<Boolean>> exList=predicate.getExpressions();//动态sql表达式集合
                if(dto.getUserName()!=null && !"".equals(dto.getUserName())){
                    exList.add(criteriaBuilder.like(root.<String>get("LoginName"),"%"+dto.getUserName()+"%"));
                    System.out.println(dto.getUserName());
                }
                if(dto.getBeginDate()!=null){
                    exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("CreateTime"), dto.getBeginDate()));
                    System.out.println(dto.getBeginDate());
                }
                if(dto.getEndDate()!=null){
                    exList.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("CreateTime"), dto.getEndDate()));
                    System.out.println(dto.getEndDate());
                }
                if(dto.getIsLock()!=null){
                    exList.add(criteriaBuilder.equal(root.<String>get("IsLockout"), dto.getIsLock()));
                    System.out.println(dto.getIsLock());
                }
                return predicate;
			}
    };
    }

    @Override
    public Users saveUsers(Users u) {
        return usersDao.save(u);
    }

    @Override
    public void delUsersById(Integer id) {
        
        Users user=usersDao.getOne(userId);
        for(String roleId:roleIdList){
            Roles role=new Roles();
            role.setId(roleId);
            user.getRolesList().add(role);
        }
        usersDao.save(user);

    }

    
    
}